import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/business_select_components/business_common_select_item.dart';
import 'package:x3_form_core/business_select_components/business_select_mixin.dart';
import 'package:x3_form_core/models/vip_select_model.dart';
import 'package:x3_form_core/provider/base_advanced_filter_provider.dart';

/// author：william on 2021/3/31 17:42
/// email：362661044@qq.com

class VipSelectPage extends StatefulWidget {
  final bool isMulti;
  final BaseHttpRequestModel request;
  final List<BaseKeyValueModel> initData;

  const VipSelectPage({Key key, @required this.isMulti, @required this.request, @required this.initData}) : super(key: key);

  @override
  _VipSelectPageState createState() => _VipSelectPageState();
}

class _VipSelectPageState extends State<VipSelectPage> with BusinessSelectMixin {
  @override
  Widget buildItem(Map<String, dynamic> item, int index) {
    VipSelectModel _model = VipSelectModel.fromJson(item);
    return BusinessCommonSelectItem<VipSelectModel>(
      isSelected: isSelected(_model),
      model: _model,
      onChange: onChange,
      child: Row(
        children: [
          Stack(
            children: [
              Align(alignment: Alignment.topCenter, child: BaseImageComponent(imageUrl: _model.vipAvatarUrl, placeholderName: _model.vipName, type: BaseImageTypeEnum.X3User, isClipOval: true, width: 40, height: 40)),
            ],
          ),
          SizedBox(width: 12),
          Expanded(child: _buildContent(_model)),
        ],
      ),
    );
  }

  Widget _buildContent(VipSelectModel model) {
    return DefaultTextStyle(
      style: Theme.of(context).textTheme.subtitle2,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          BaseComponentUtils.buildTextSpanWidget([model.vipName, model.gradeName], styles: {0: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 14.0), 1: Theme.of(context).textTheme.subtitle1}),
          SizedBox(height: 4.0),
          BaseComponentUtils.buildTextSpanWidget(['卡号:', model.vipId]),
          SizedBox(height: 4.0),
          BaseComponentUtils.buildTextSpanWidget(['手机号:', model.vipMobile]),
        ],
      ),
    );
  }

  @override
  String get inputApi => widget.request?.inputApi ?? '/retail/retails/cashier/v2/vipinfo/query';

  @override
  String get title => '选择会员';

  @override
  bool get isMulti => widget.isMulti;

  @override
  List<BaseKeyValueModel> get initData => widget.initData ?? [];

  @override
  String get businessTabEnum => "user";

  @override
  Map<String, dynamic> get params => {'searchKey': ''};

  @override
  List<BaseKeyValueModel> typeConversion(List<Map<String, dynamic>> items) {
    List<BaseKeyValueModel> _list = [];
    items.forEach((element) {
      VipSelectModel _model = VipSelectModel.fromJson(element);
      _list.add(_model);
    });
    return _list;
  }

  @override
  BaseScreenRequestModel get filterRequestModel => null;

  @override
  BaseAdvancedFilterProvider initFilterDataSource(BaseHttpResultModel result) => null;
}
